package com.dvd.dao.dvd;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.*;
/**
 * A data access object (DAO) providing persistence and search support for TDvd
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.dvd.dao.dvd.TDvd
 * @author MyEclipse Persistence Tools
 */

public class TDvdDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TDvdDAO.class);
	// property constants
	public static final String _FDVD_TITLE = "FDvdTitle";
	public static final String _FYEAR = "FYear";
	public static final String _FTYPE = "FType";
	public static final String _FCOUNTRY = "FCountry";
	public static final String _FPRICE = "FPrice";
	public static final String _FIMG_PATH = "FImgPath";
	public static final String _FDIRECTOR = "FDirector";
	public static final String _FACTOR = "FActor";
	public static final String _FMEMO = "FMemo";

	public void save(TDvd transientInstance) {
		log.debug("saving TDvd instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TDvd persistentInstance) {
		log.debug("deleting TDvd instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TDvd findById(java.lang.String id) {
		log.debug("getting TDvd instance with id: " + id);
		try {
			TDvd instance = (TDvd) getSession().get("com.dvd.dao.dvd.TDvd", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
	/*************************************************************/
	public TDvd findPreById(java.lang.String id) {
		log.debug("getting previous TDvd instance with id: " + id);
		try {
			//TDvd instance = (TDvd) getSession().createQuery("");
			TDvd instance=null;
			Criteria criteria =this.getSession().createCriteria(TDvd.class);
			criteria.
			add(Expression.lt("FDvdId", id)).
			addOrder(Order.desc("FDvdId")).
			setFirstResult(0).
			setMaxResults(1);
			List<?> list=(List<?>)criteria.list();
			instance= list.isEmpty() ? null : (TDvd)list.get(0);
		
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public TDvd findNextById(java.lang.String id) {
		log.debug("getting next TDvd instance with id: " + id);
		try {
			TDvd instance=null;
			Criteria criteria =this.getSession().createCriteria(TDvd.class);
			criteria.
			add(Expression.gt("FDvdId", id)).
			addOrder(Order.asc("FDvdId")).
			setFirstResult(0).
			setMaxResults(1);
			
			List<?> list=(List<?>)criteria.list();
			instance= list.isEmpty() ? null : (TDvd)list.get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<TDvd> searchDvdList(int pageId,int pageCount) {
		
		log.debug("getting TDvd list  with pageId: " + pageId);
		try {
			Criteria criteria =this.getSession().createCriteria(TDvd.class);
			criteria.
			addOrder(Order.asc("FDvdId")).
			setFirstResult(pageCount * (pageId-1)).
			setMaxResults(15);
			
			List<TDvd> list=(List<TDvd>)criteria.list();
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
		
	}
	
	public TDvd findByTitle(java.lang.String title) {
		log.debug("getting next TDvd instance with title: " + title);
		try {
			TDvd instance=null;
			Criteria criteria =this.getSession().createCriteria(TDvd.class);
			criteria.
			add(Expression.eq("FDvdTitle", title)).
			setFirstResult(0).
			setMaxResults(1);
			
			List<?> list=(List<?>)criteria.list();
			instance= list.isEmpty() ? null : (TDvd)list.get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/*************************************************************/
	
	
	

	public List findByExample(TDvd instance) {
		log.debug("finding TDvd instance by example");
		try {
			List results = getSession().createCriteria("com.dvd.dao.dvd.TDvd")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TDvd instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TDvd as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByFDvdTitle(Object FDvdTitle) {
		return findByProperty(_FDVD_TITLE, FDvdTitle);
	}

	public List findByFYear(Object FYear) {
		return findByProperty(_FYEAR, FYear);
	}

	public List findByFType(Object FType) {
		return findByProperty(_FTYPE, FType);
	}

	public List findByFCountry(Object FCountry) {
		return findByProperty(_FCOUNTRY, FCountry);
	}

	public List findByFPrice(Object FPrice) {
		return findByProperty(_FPRICE, FPrice);
	}

	public List findByFImgPath(Object FImgPath) {
		return findByProperty(_FIMG_PATH, FImgPath);
	}

	public List findByFDirector(Object FDirector) {
		return findByProperty(_FDIRECTOR, FDirector);
	}

	public List findByFActor(Object FActor) {
		return findByProperty(_FACTOR, FActor);
	}

	public List findByFMemo(Object FMemo) {
		return findByProperty(_FMEMO, FMemo);
	}
	
	
	public List findAll() {
		log.debug("finding all TDvd instances");
		try {
			String queryString = "from TDvd";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TDvd merge(TDvd detachedInstance) {
		log.debug("merging TDvd instance");
		try {
			TDvd result = (TDvd) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TDvd instance) {
		log.debug("attaching dirty TDvd instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TDvd instance) {
		log.debug("attaching clean TDvd instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}